<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\BookCate;
use Illuminate\Http\Request;
use function App\Helpers\errorMsg;
use function App\Helpers\successMsg;

class BookCateController extends Controller
{
    //
    public function list()
    {
        $data = BookCate::getBookCateByPid(0);
        return successMsg($data);
    }
    public function insert(Request $request)
    {
        $pid = $request->pid ?? 0;
        $name = $request->name ?? '';
        $data = [
            'pid' => $pid,
            'name' => $name,
        ];
        BookCate::query()->insert($data);
        return successMsg();
    }
    public function update(Request $request)
    {
        $id = $request->id ?? 0;
        $pid = $request->pid ?? 0;
        $name = $request->name ?? '';
        if($id == 0) return errorMsg(-1,"id传值 不能为空 或 0");
        $data = [
            'pid' => $pid,
            'name' => $name,
        ];
        BookCate::query()->where('id',$id)->update($data);
        return successMsg();
    }
    public function delete(Request $request)
    {
        $ids = $request->ids ?? null;
        $cate_ids = is_array($ids) ? $ids : explode(",",$ids);
        if(in_array(1,$cate_ids) || in_array(1,$cate_ids)) errorMsg(-1,'男频/女频 不能删除');
        $row = BookCate::query()->whereIn('id',$cate_ids)->delete();
        if(!$row) return errorMsg(-1,'删除失败');
        return successMsg();
    }
}
